package com.baizan.daka.ui.activity

import android.content.Context
import android.content.Intent
import android.view.View
import com.baizan.daka.base.BasePageListActivity
import com.baizan.daka.event.TaskEvent
import com.baizan.daka.mvp.model.bean.TaskInfoBean
import com.baizan.daka.net.BasePageResponse
import com.baizan.daka.showToast
import com.baizan.daka.ui.adapter.TaskAdapter
import com.baizan.daka.ui.contract.TaskHomeContract
import com.baizan.daka.ui.dialog.TaskSelectDialog
import com.baizan.daka.ui.presenter.TaskHomePresenter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_task_home.*
import org.greenrobot.eventbus.EventBus

class TaskHomeActivity : BasePageListActivity<TaskHomePresenter, TaskAdapter>(),
    TaskHomeContract.View {
    var level: Int? = null
    var taskClass: Int? = null
    var levelIndex :Int? = null
    var taskClassIndex :Int? = null
    val taskNameData: List<String> = listOf("不限", "初级任务", "中级任务", "高级任务","至尊任务")
    val taskTypeData: List<String> = listOf("不限", "抖音任务", "快手任务","朋友圈任务")
    companion object {
        fun start(context: Context, level: Int) {
            val intent = Intent(context, TaskHomeActivity::class.java)
            intent.putExtra("level", level)
            context.startActivity(intent)
        }
    }

    override fun onRefresh() {
        presenter?.loadList(mCurrentPageNo, level, taskClass)
    }

    override fun onLoadMore() {
        presenter?.loadList(mCurrentPageNo, level, taskClass)
    }

    override fun initAdapter(): TaskAdapter = TaskAdapter()

    override fun initPresenter(): TaskHomePresenter = TaskHomePresenter(this)

    override fun initView() {
       val leveltemp = intent.getIntExtra("level", 0)
        levelIndex = leveltemp
        task_level_tv.text = taskNameData[leveltemp]
        this.level = leveltemp
        task_level_tv.setOnClickListener {
            it.isSelected = true
            val dialog = TaskSelectDialog(this, taskNameData,levelIndex)
            dialog.listener = BaseQuickAdapter.OnItemClickListener { _, _, i: Int ->
                levelIndex = i
                when (i) {
                    0 -> {
                        level = null
                        task_level_tv.text = "任务级别"
                    }
                    1->{
                        task_level_tv.text = taskNameData[i]
                        level = i
                    }
                    2->{
                        task_level_tv.text = taskNameData[i]
                        level = i
                    }
                    3->{
                        task_level_tv.text = taskNameData[i]
                        level = i
                    }
                    4->{
                        task_level_tv.text = taskNameData[i]
                        level = i
                    }

                }
                dialog.dismiss()
                presenter?.loadList(mCurrentPageNo,level, taskClass)
            }
            dialog.showPopupWindow(it)
        }
        task_type_tv.setOnClickListener {
            it.isSelected = true
            var type :Int = 0
            task_type_tv.getTag()?.let {
                type  =it.toString().toInt()
            }
            val dialog = TaskSelectDialog(this, taskTypeData,type)
            dialog.listener = BaseQuickAdapter.OnItemClickListener { _, _, i: Int ->
                task_type_tv.setTag(i)
                taskClassIndex = i
                when(i){
                    0->{
                        task_type_tv.text = "任务类型"
                        taskClass = null
                    }
                    1->{
                        task_type_tv.text = taskTypeData[i]
                        taskClass = i
                    }
                    2->{
                        task_type_tv.text = taskTypeData[i]
                        taskClass = i
                    }
                    3->{
                        task_type_tv.text = taskTypeData[i]
                        taskClass = 0
                    }
                }

                dialog.dismiss()
                presenter?.loadList(mCurrentPageNo,level, taskClass)
            }
            dialog.showPopupWindow(it)
        }
        adapter?.setOnItemClickListener { adapter, _, position ->
            val item = adapter.data.get(position)
            item as TaskInfoBean
            presenter?.receiveTask(position,item.id)
        }
    }

    override fun start() {
        presenter?.loadList(mCurrentPageNo, level, taskClass)
    }

    override fun loadListSuccess(data: BasePageResponse<TaskInfoBean>) {
        getDatasSuccess(data)
    }

    override fun loadListFail(fail: String) {
        getDatasFail()
    }

    override fun receiveTaskSuccess(index:Int,taskInfoBean: Any) {
        adapter?.setReceived(index)
        EventBus.getDefault().post(TaskEvent())
        showToast("领取成功")
    }

    override fun layoutId(): Int = R.layout.act_task_home
}